package se.rkrantz.simpleraid.web.controller.raid;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import se.rkrantz.simpleraid.service.raid.RaidService;

@Controller
public class RaidController {

	@Autowired private RaidService raidService;

	@RequestMapping("/calendar.html")
	public String getRaids(Model model) {
		
		DateTime startDate = new DateTime();
		DateTime endDate = new DateTime();
		
		startDate = startDate.minusWeeks(1).withDayOfWeek(1);
		endDate = endDate.plusWeeks(2).withDayOfWeek(7);
		
		model.addAttribute("weeks", raidService.getVisibleCalendarWeeks());
		model.addAttribute("raids", raidService.getRaids(startDate, endDate));

		return "calendar";
	}
	
	@RequestMapping("/raid.html")
	public String getRaid(@RequestParam long raidId, Model model) {
		model.addAttribute("raid", raidService.getRaid(raidId));
		
		return "raid";
	}
	
	@RequestMapping("createRaid.html")
	public String createRaid(@RequestParam String raidTitle, @RequestParam String raidDate, Model model) {
		raidService.createRaid(raidTitle, raidDate);
		
		return "redirect:calendar.html";
	}
	
	@RequestMapping("signup.html")
	public String signup(@RequestParam long raidId, @RequestParam long characterId, @RequestParam String role, @RequestParam String status, Model model) {
		
		raidService.signupToRaid(raidId, characterId, role, status);
		
		return "redirect:calendar.html";
	}
}
